package com.ferryc.modules.sys.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ferryc.commons.entity.LayTree;
import com.ferryc.commons.entity.TreeNode;
import com.ferryc.commons.service.CommonService;
import com.ferryc.commons.utils.StringUtil;
import com.ferryc.constant.GlobalConstant;
import com.ferryc.modules.sys.entity.SysMenu;
import com.ferryc.modules.sys.mapper.ISysMenuMapper;
import com.ferryc.modules.sys.service.ISysMenuService;

/**
 * 
 * 类名: ISysMenuService <br/>
 * 作用： 服务<br/>
 * 方法： <br/>
 * 创建者: Ferry C <br/>
 * 添加时间: 2017年11月26日 04:09:02 <br/>
 * 
 */
@Service
@Transactional(readOnly = true)
public class SysMenuServiceImpl extends CommonService<SysMenu> implements ISysMenuService {

	@Autowired
	private ISysMenuMapper sysMenuMapper;

	@Override
	protected void init() {
		super.mapper = sysMenuMapper;
	}

	@Override
	public List<TreeNode> selectTreeList(SysMenu sysMenu) {
		sysMenu.setStatus(GlobalConstant.YES);
		sysMenu.setDelFlag(GlobalConstant.NO);
		List<SysMenu> list = sysMenuMapper.listByEntity(sysMenu);
		return createNode(list, SysMenu.SYSTEM_PARENT_ID);
	}

	/**
	 * 创建树
	 * 
	 * @param menuList
	 * @param parentId
	 * @return
	 */
	private List<TreeNode> createNode(List<SysMenu> menuList, String parentId) {
		List<TreeNode> nodeList = new ArrayList<>();
		for (Iterator iterator = menuList.iterator(); iterator.hasNext();) {
			SysMenu sysMenu = (SysMenu) iterator.next();
			if (parentId.equals(sysMenu.getParentId())) {
				TreeNode treeNode = new TreeNode();
				treeNode.setId(sysMenu.getId());
				treeNode.setText(sysMenu.getMenuName());
				if (GlobalConstant.YES.equals(sysMenu.getSelected())) {
					treeNode.getState().setSelected(Boolean.TRUE);
				}
				if (SysMenu.SYSTEM_ID.equals(sysMenu.getParentId()) || SysMenu.SYSTEM_ID.equals(sysMenu.getId())) {
					treeNode.setIcon(StringUtil.isNotBlank(sysMenu.getIcon()) ? sysMenu.getIcon() : TreeNode.MENU_DEFAULT_ICON);
				}
				boolean isParent = checkParent(menuList, sysMenu.getId());
				if (isParent) {
					treeNode.setChildren(createNode(menuList, sysMenu.getId()));
				}
				nodeList.add(treeNode);
				treeNode = null;
			}
		}
		return nodeList;
	}

	/**
	 * 检验是否为父节点
	 * 
	 * @param menuList
	 * @param id
	 * @return
	 */
	private boolean checkParent(List<SysMenu> menuList, String id) {
		for (Iterator it = menuList.iterator(); it.hasNext();) {
			SysMenu menu = (SysMenu) it.next();
			if (id.equals(menu.getParentId())) {
				return Boolean.TRUE;
			}
		}
		return Boolean.FALSE;
	}

	@Override
	public boolean checkMenuName(SysMenu sysMenu) {
		SysMenu queryMenu = new SysMenu();
		queryMenu.setStatus(StringUtil.EMPTY);
		queryMenu.setDelFlag(GlobalConstant.DEL_FLAG_NORMAL);
		queryMenu.setType(SysMenu.MenuType.MENU.getValue());
		queryMenu.setMenuName(sysMenu.getMenuName());
		List<SysMenu> list = sysMenuMapper.listByEntity(queryMenu);
		if (CollectionUtils.isEmpty(list)) {
			return Boolean.TRUE;
		}
		if (StringUtil.isBlank(sysMenu.getId())) {
			return Boolean.FALSE;
		} else {
			if (list.size() == 1 && list.get(0).getId().equals(sysMenu.getId())) {
				return Boolean.TRUE;
			}
			return Boolean.FALSE;
		}
	}

	@Override
	public boolean checkPermission(SysMenu sysMenu) {
		SysMenu queryMenu = new SysMenu();
		queryMenu.setStatus(StringUtil.EMPTY);
		queryMenu.setDelFlag(GlobalConstant.DEL_FLAG_NORMAL);
		queryMenu.setPermission(sysMenu.getPermission());
		List<SysMenu> list = sysMenuMapper.listByEntity(queryMenu);
		if (CollectionUtils.isEmpty(list)) {
			return Boolean.TRUE;
		}
		if (StringUtil.isBlank(sysMenu.getId())) {
			return Boolean.FALSE;
		} else {
			if (list.size() == 1 && list.get(0).getId().equals(sysMenu.getId())) {
				return Boolean.TRUE;
			}
			return Boolean.FALSE;
		}
	}

	@Override
	public List<LayTree> selectTreePermList(String permissionsId) {
		List<SysMenu> menuPremList = sysMenuMapper.getMenuPermList(permissionsId);
		return createMenu(menuPremList, SysMenu.SYSTEM_PARENT_ID);
	}

	@Override
	public boolean checkChild(SysMenu sysMenu) {
		SysMenu queryMenu = new SysMenu();
		queryMenu.setParentId(sysMenu.getId());
		queryMenu.setStatus(StringUtil.EMPTY);
		queryMenu.setDelFlag(GlobalConstant.DEL_FLAG_NORMAL);
		int result = sysMenuMapper.countByEntity(queryMenu);
		if (result > 0) {
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

	@Override
	public List<SysMenu> getUserMenuById(String userId) {
		return sysMenuMapper.getUserMenuById(userId);
	}

	private List<LayTree> createMenu(List<SysMenu> mList, String parentId) {
		List<LayTree> lTree = new ArrayList<LayTree>();

		for (Iterator iterator = mList.iterator(); iterator.hasNext();) {
			SysMenu sysMenu = (SysMenu) iterator.next();
			if (parentId.equals(sysMenu.getParentId())) {
				LayTree lt = new LayTree();
				lt.setName(sysMenu.getMenuName());
				lt.setChecked(Boolean.FALSE);
				if (GlobalConstant.YES.equals(sysMenu.getSelected())) {
					lt.setChecked(Boolean.TRUE);
				}
				lt.setSpread(true);
				lt.setCheckboxValue(sysMenu.getId());
				boolean isParent = checkParent(mList, sysMenu.getId());
				if (isParent) {
					lt.setChildren(createMenu(mList, sysMenu.getId()));
				}
				lTree.add(lt);
				lt = null;
			}
		}
		return lTree;
	}
}